import scrapy
from ..items import TencentrecruitItem


class HrSpider(scrapy.Spider):
    name = 'hr'
    allowed_domains = ['tencent.com']
    start_urls = ['https://careers.tencent.com/search.html']

    def parse(self, response, **kwargs):
        req = scrapy.Request(
            url=self.api_parameters_process(),
            callback=self.parse_json,
        )
        yield req

    def api_parameters_process(self, pageIndex=1, pageSize: "这个的取值代表爬取多少信息 范围为100~10000" = 100):
        """因为腾讯招聘已经换了渲染方式，现在可以直接调用接口就行了"""
        url = f"https://careers.tencent.com/tencentcareer/api/post/Query?pageIndex={pageIndex}&pageSize={pageSize}"
        return url

    def parse_json(self, response):
        content_list = response.json().get("Data").get("Posts")
        for each in content_list:
            address = each.get("CountryName") + "--->" + each.get("LocationName")
            work = each.get("CategoryName")
            work_details = each.get("Responsibility")
            date = each.get("LastUpdateTime")
            url = each.get("PostURL")
            item = TencentrecruitItem(
                address=address,
                work=work,
                work_details=work_details,
                date=date,
                url=url,
            )
            yield item
